$(function() {
	
	function updateAnalytics() {
		$.post("/getDistribution", {courseKey: $("#storeCourse").attr("title")},
		function(data) {
			var rightNum = {label: "Correct", data: []};
			var wrongNum = {label: "Wrong", data: []};
			var rightPer = {label: "Percent Correct", data: []};
			var xAxisLabels = {ticks: []};
			for (var i = 0; i < data.length; i++) {
				rightNum["data"].push([i, data[i][1]]);
				wrongNum["data"].push([i, data[i][2] - data[i][1]]);
				rightPer["data"].push([i, data[i][1]/data[i][2]]);
				xAxisLabels["ticks"].push([i, data[i][0]]);
			}
			$.plot($("#responseNumberHolder"), [ rightNum, wrongNum ], {
				series: {
					stack: 0,                
					lines: { show: false, steps: false},
					bars: { show: true, barWidth: 0.9, align: "center"},
				},
				xaxis: xAxisLabels,
				yaxis: { min:0 },
			});
			
			$.plot($("#responsePercentHolder"), [rightPer], {
				series: {               
					lines: { show: false, fill: true, steps: false},
					bars: { show: true, barWidth: 0.9, align: "center"},
				},
				xaxis: xAxisLabels,
				yaxis: { min:0, max: 1, tickSize: .1},
			});
			}, "json" );
		}
		
		
	if ($(".analyticsCourse").length > 0) {
		//window.setInterval(updateAnalytics, 1000);
		updateAnalytics();
	}
});